//
//  UploadSecondViewController.m
//  LoveAttention
//
//  Created by 于金祥 on 16/6/2.
//  Copyright © 2016年 JC. All rights reserved.
//

#import "UploadSecondViewController.h"
#import "CommonDef.h"
#import "DataProvider.h"
#import "UploadThirdViewController.h"

@interface UploadSecondViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

@end

@implementation UploadSecondViewController
{
    UITextField * txt_hangye;
    UILabel * lbl_zhekou;
    NSArray * array_hangye;
    NSDictionary * secondDict;
    
    UIView *BackView;
    UIPickerView *_mypicker;
    
    NSString * firstid;
    NSString * firstName;
    
    NSString * secondid;
    NSString * secondName;
    
    NSString * discount;
    
    BOOL isSelected;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    isSelected=NO;
    array_hangye=[[NSArray alloc] init];
    secondDict=[[NSDictionary alloc] init];
    [self GetData];
    [self p_navigationController];
    [self BuildAllView];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navigationController
- (void)p_navigationController
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.navigationItem.title = @"选择您的行业与结算折扣";
    
    //    self.navigationItem.leftBarButtonItem
    
    
    UIButton * btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"返回"] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(10, 20, 25, 30);
    [btn addTarget:self action:@selector(leftBarButtonItemAction:) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    
    //    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightBarButtonItemAction:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor blackColor];
    //    self.navigationItem.rightBarButtonItem.tintColor = [UIColor whiteColor];
}
//点击事件(左)
- (void)leftBarButtonItemAction:(UIBarButtonItem *)bar
{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)BuildAllView
{
    txt_hangye=[[UITextField alloc] initWithFrame:CGRectMake(0, 74, SCREEN_WIDTH, 44)];
    txt_hangye.backgroundColor=[UIColor whiteColor];
    txt_hangye.font=[UIFont systemFontOfSize:12];
    txt_hangye.text=@"  点击选择您的行业与折扣";
    UIImageView * img_right=[[UIImageView alloc] initWithFrame:CGRectMake(0, 12, 20, 20)];
    img_right.image=[UIImage imageNamed:@"go_icon"];
    txt_hangye.rightView =img_right;
    txt_hangye.rightViewMode=UITextFieldViewModeAlways;
    [self.view addSubview:txt_hangye];
    UIButton * btn_select=[[UIButton alloc] initWithFrame:txt_hangye.bounds];
    [btn_select addTarget:self action:@selector(ShowSelect:) forControlEvents:UIControlEventTouchUpInside];
    [txt_hangye addSubview:btn_select];
    lbl_zhekou=[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(txt_hangye.frame)+1, SCREEN_WIDTH, 44)];
    lbl_zhekou.font=[UIFont systemFontOfSize:15];
    lbl_zhekou.text=@"  9";
    lbl_zhekou.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:lbl_zhekou];
    UILabel * lbl_tishi=[[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lbl_zhekou.frame), SCREEN_WIDTH, 19)];
    lbl_tishi.text=@"  例如:结算折扣填写8.5，表示8.5折";
    lbl_tishi.textColor=[UIColor orangeColor];
    [self.view addSubview:lbl_tishi];
    UIButton * btn_next=[[UIButton alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(lbl_tishi.frame)+30, SCREEN_WIDTH-30, 30)];
    btn_next.layer.masksToBounds=YES;
    btn_next.layer.cornerRadius=5;
    btn_next.backgroundColor=[UIColor orangeColor];
    [btn_next setTitle:@"下一步" forState:UIControlStateNormal];
    [btn_next addTarget:self action:@selector(NextStep) forControlEvents:UIControlEventTouchUpInside];
    [btn_next setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:btn_next];
}
-(void)NextStep
{
    if (isSelected) {
        UploadThirdViewController *uploadNext=[[UploadThirdViewController alloc] init];
        uploadNext.secondid=secondid;
        [self showViewController:uploadNext sender:nil];
    }
    else
    {
        UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请选择行业" preferredStyle:(UIAlertControllerStyleAlert)];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        UIAlertAction * action = [UIAlertAction actionWithTitle:@"好的" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        [alert addAction:action];
    }
}
-(void)GetData
{
    DataProvider * dataprovider=[[DataProvider alloc] init];
    [dataprovider setDelegateObject:self setBackFunctionName:@"GetDataCallBack:"];
    [dataprovider GetHangye];
}

-(void)GetDataCallBack:(id)dict
{
    DLog(@"%@",dict);
    if ([dict[@"status"][@"succeed"] intValue]==1) {
        array_hangye=dict[@"data"];
        secondDict=array_hangye[0];
        if (array_hangye.count>0) {
            firstid=array_hangye[0][@"id"];
            firstName=array_hangye[0][@"name"];
        }
        NSArray * itemArray=[[NSArray alloc] initWithArray:secondDict[@"secondList"]];
        if (itemArray.count>0) {
            secondid=secondDict[@"secondList"][0][@"id"];
            secondName=secondDict[@"secondList"][0][@"name"];
            discount=secondDict[@"secondList"][0][@"discount"];
        }
        
    }
    else
    {
        
    }
}

-(void)ShowSelect:(UIButton *)sender
{
    if (array_hangye.count>0) {
        BackView=[[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-250, SCREEN_WIDTH, 50)];
        [BackView setBackgroundColor:[UIColor whiteColor]];
        UIButton * btn_cancel=[[UIButton alloc] initWithFrame:CGRectMake(10, 0, 60, 50)];
        [btn_cancel setTitle:@"取消" forState:UIControlStateNormal];
        [btn_cancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn_cancel addTarget:self action:@selector(cancelSelect:) forControlEvents:UIControlEventTouchUpInside];
        UIButton * btn_sure=[[UIButton alloc] initWithFrame:CGRectMake(BackView.frame.size.width-70, 0, 60, 50)];
        [btn_sure setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn_sure setTitle:@"确定" forState:UIControlStateNormal];
        [btn_sure addTarget:self action:@selector(sureForSelect:) forControlEvents:UIControlEventTouchUpInside];
        UIView * fenge=[[UIView alloc] initWithFrame:CGRectMake(0, BackView.frame.size.height-1, BackView.frame.size.width, 1)];
        fenge.backgroundColor=[UIColor grayColor];
        [BackView addSubview:btn_sure];
        [BackView addSubview:btn_cancel];
        [BackView addSubview:fenge];
        [self.view addSubview:BackView];
        
        
        _mypicker=[[UIPickerView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-200, SCREEN_WIDTH, 200)];
        _mypicker.delegate=self;
        _mypicker.dataSource=self;
        _mypicker.backgroundColor=[UIColor grayColor];
        [self.view addSubview:_mypicker];
    }
    else
    {
        [self GetData];
    }
}

#pragma mark--UIPickerViewDataSource
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    @try {
        if (component==0) {
            return [array_hangye count];
        }
        if (component==1) {
            return [secondDict[@"secondList"] count];
        }
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    return 0;
}

#pragma mark--UIPickerViewDelegate
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    @try {
        if (component==0) {
            return [array_hangye objectAtIndex:row][@"name"];
        }
        if (component==1) {
            
            return secondDict[@"secondList"][row][@"name"];;
        }
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    return nil;
}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    @try {
        NSLog(@"row is %ld,Component is %ld",(long)row,(long)component);
        if (component==0) {
            secondDict=array_hangye[row];
            [_mypicker reloadComponent:1];
            firstid=array_hangye[row][@"id"];
            firstName=array_hangye[row][@"name"];
        }
        if (component==1) {
            secondid=secondDict[@"secondList"][row][@"id"];
            secondName=secondDict[@"secondList"][row][@"name"];
            discount=secondDict[@"secondList"][row][@"discount"];
        }
    }
    @catch (NSException *exception) {
        
    }
    @finally {
        
    }
    
}


-(void)cancelSelect:(UIButton * )sender
{
    [BackView removeFromSuperview];
    [_mypicker removeFromSuperview];
}
-(void)sureForSelect:(UIButton *)sender
{
    txt_hangye.text=[NSString stringWithFormat:@"  %@->%@",firstName,secondName];
    lbl_zhekou.text=[NSString stringWithFormat:@"  %@",discount];
    [BackView removeFromSuperview];
    [_mypicker removeFromSuperview];
    isSelected=YES;
}

@end
